import {
  Column,
  PrimaryColumn,
  Generated,
  ManyToMany,
  JoinTable,
  ManyToOne,
  JoinColumn,
  CreateDateColumn,
  UpdateDateColumn,
  OneToMany
} from 'typeorm'
import { EntityModel } from '@midwayjs/orm'
import { TicketSource } from './ticket_source'
import { TicketSign } from './ticket_sign'
import { Attachment } from './attachment'
import { Department } from './department'
import { SystemUser } from './system_user'

@EntityModel('ticket')
export class Ticket {

  @PrimaryColumn({ type: 'uuid' })
  @Generated('uuid')
  id: string;

  @Column({
    comment: `工单状态，
      0: APPLIED - 待审核
      1: CANCELLED - 审核未通过
      2: PENDING - 待处理
      3: PROCESSING - 处理中
      4: DEPRECATED - 不予受理
      5: WITHDRAW - 已撤回
      6: FAILED - 调解不成
      7: COMPLETED - 已办结
    `,
    type: 'smallint',
    default: 0
  })
  status: number;

  @Column({
    comment: '工单名称'
  })
  title: string;

  @Column({
    comment: '编号',
    nullable: true
  })
  code: string;

  @Column({
    comment: '工单内容'
  })
  content: string;

  @ManyToOne(() => TicketSource, { nullable: false })
  @JoinColumn({ name: 'ticket_source_id' })
  source: TicketSource;

  // 工单所属部门，默认为 create_by 所属部门，或可选择为其部门子部门
  @ManyToOne(() => Department, { nullable: false })
  @JoinColumn({ name: 'department_id' })
  department: Department;

  @Column({
    type: 'jsonb',
    default: [],
    comment: '保存当前书签 + 签名'
  })
  bookmarks: any[];

  // 事件举证
  @ManyToMany(() => Attachment)
  @JoinTable({
    name: 'ticket_attachments',
    joinColumn: {
      name: 'ticket_id',
      referencedColumnName: 'id'
    },
    inverseJoinColumn: {
      name: 'attachment_id',
      referencedColumnName: 'id'
    }
  })
  attachments: Attachment[];

  // 涉案材料
  @ManyToMany(() => Attachment)
  @JoinTable({
    name: 'ticket_attachments_pending',
    joinColumn: {
      name: 'ticket_id',
      referencedColumnName: 'id'
    },
    inverseJoinColumn: {
      name: 'attachment_id',
      referencedColumnName: 'id'
    }
  })
  attachments_pending: Attachment[];

  // 待签字文件
  @OneToMany(() => TicketSign, ticketSign => ticketSign.ticket)
  attachments_sign: TicketSign[];

  // 受理
  @Column({
    type: 'timestamp',
    nullable: true,
    comment: '受理时间'
  })
  accept_at: Date;

  @ManyToOne(() => SystemUser)
  @JoinColumn({ name: 'accept_by_id' })
  accept_by: SystemUser;

  @Column({
    comment: '受理补充已经，一般为不受理原因',
    nullable: true
  })
  accept_msg: string;

  // 处理
  @Column({
    type: 'timestamp',
    nullable: true,
    comment: '受理时间'
  })
  process_at: Date;

  @ManyToOne(() => SystemUser)
  @JoinColumn({ name: 'process_by_id' })
  process_by: SystemUser;

  @Column({
    comment: '处理意见',
    nullable: true
  })
  process_msg: string;

  @CreateDateColumn({
    comment: '创建时间'
  })
  create_at: Date;

  // 创建
  @ManyToOne(() => SystemUser)
  @JoinColumn({ name: 'create_by_id' })
  create_by: SystemUser;

  @Column({
    comment: '备注，可为联系方式等',
    nullable: true
  })
  create_msg: string;

  @Column({
    type: 'timestamp',
    nullable: true,
    comment: '软删除标记'
  })
  deleted_at: Date;

  @UpdateDateColumn({
    comment: '更新时间'
  })
  update_at: Date;
}
